package info.fastpace.android.utils;

import info.fastpace.utils.Config;

import java.io.File;

import android.content.pm.PackageManager;
import android.os.Environment;
import android.provider.Settings.Secure;

public class DeviceUtils {

	private static String phoneId = null; // default value if android id is not found
	
	public static String getPhoneId() {
		if (phoneId != null) {
			return phoneId;
		}
		String phoneId = Secure.getString(Application.get().getContentResolver(), Secure.ANDROID_ID);
		if (phoneId == null) {
			phoneId = "Android";
		}
		DeviceUtils.phoneId = phoneId;
		
		return phoneId;
	}

	public static boolean isExternalStorageMounted() {
		String state = Environment.getExternalStorageState();
		if (!Environment.MEDIA_MOUNTED.equals(state)) {
			return false;
		}

		// Checking for external cache dir is needed because we saw a market error of a device
		// that the external storage was mounted
		// but the cache returned null
		if (Application.get().getExternalCacheDir() == null) {
			Config.getLog().e("External storage Mounted but the external cache dir is null");
			return false;
		}

		return true;
	}

	public static File getExternalCachDir() {
		if (!isExternalStorageMounted()) {
			return null;
		}
		File externalCacheDir = Application.get().getExternalCacheDir();
		externalCacheDir.mkdirs();
		if (!externalCacheDir.isDirectory()) {
			externalCacheDir = null;
		}
		return externalCacheDir;
	}

	public static boolean is3gSupported() {
		return Application.get().getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
	}

	public static boolean isCameraSupported() {
		return Application.get().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
	}
}
